# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: leon

from absl import app, flags

import pandas
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

FLAGS = flags.FLAGS

QUOTE_LIST = ['USDT', 'USD', 'EUR', 'BTC', 'ETH', 'KRW']
QUOTE_EXCHANGE = {'USDT': 1, 'USD': 1, 'EUR': 1.17, 'BTC': 7000, 'ETH': 450, 'KRW': 0.00088}


def plot_hist(feed_stats):
  plt.figure(figsize=(10, 10))
  plt.title('hist_no_book_duration')
  plt.hist(feed_stats.loc[(feed_stats['volume_usd'] > 100000) & (feed_stats['book_count'] > 20000),
                          'no_book_duration'],
           bins=50)
  plt.yscale('log', basey=2)
  plt.savefig('hist/hist_no_book_duration.png')

  plt.figure(figsize=(10, 10))
  plt.title('hist_no_trade_duration')
  plt.hist(feed_stats.loc[(feed_stats['volume_usd'] > 100000) & (feed_stats['trade_count'] > 3000),
                          'no_trade_duration'],
           bins=50)
  plt.yscale('log', basey=2)
  plt.savefig('hist/hist_no_trade_duration.png')

  plt.figure(figsize=(10, 10))
  plt.title('max_avg_between_book_trade_duration')
  plt.hist(feed_stats.loc[feed_stats['max_between_book_trade_duration'] > 5,
                          'max/avg_between_book_trade_count'])
  plt.savefig('hist/max_avg_between_book_trade_duration.png')

  plt.figure(figsize=(10, 10))
  plt.title('max_avg_outside_bid_ask_trade_count')
  plt.hist(feed_stats.loc[feed_stats['max_outside_bid_ask_trade_duration'] > 5,
                          'max/avg_outside_bid_ask_trade_count'])
  plt.savefig('hist/max_avg_outside_bid_ask_trade_count.png')

  plt.figure(figsize=(10, 10))
  plt.title('trade_book_ratio')
  plt.hist(feed_stats['trade_book_ratio'], bins=50)
  plt.yscale('log', basey=2)
  plt.savefig('hist/trade_book_ratio.png')


def calc_columns(feed_stats):
  percentiles = [.25, .5, .75, .95, .99]
  print(feed_stats.loc[(feed_stats['volume_usd'] > 100000) & (feed_stats['book_count'] > 20000),
                       'no_book_duration'].describe(percentiles=percentiles))
  print(feed_stats.loc[(feed_stats['volume_usd'] > 100000) & (feed_stats['trade_count'] > 3000),
                       'no_trade_duration'].describe(percentiles=percentiles))
  print(feed_stats.loc[feed_stats['max_between_book_trade_duration'] > 5,
                       'max/avg_between_book_trade_count'].describe(percentiles=percentiles))
  print(feed_stats.loc[feed_stats['max_outside_bid_ask_trade_duration'] > 5,
                       'max/avg_outside_bid_ask_trade_count'].describe(percentiles=percentiles))
  print(feed_stats['trade_book_ratio'].describe(percentiles=percentiles))


def calc_and_gen_columns(feed_stats):
  feed_stats.dropna(how='any', inplace=True)
  feed_stats = feed_stats.loc[feed_stats['quote'].isin(QUOTE_LIST)]
  pandas.options.mode.chained_assignment = None  # default='warn'
  feed_stats['quote_exchange'] = feed_stats['quote'].map(QUOTE_EXCHANGE)
  feed_stats['volume_usd'] = feed_stats['volume'] * feed_stats['quote_exchange']
  feed_stats['max/avg_between_book_trade_count'] = (feed_stats['max_between_book_trade_count']
                                                    / feed_stats['avg_between_book_trade_count'])
  feed_stats['max/avg_outside_bid_ask_trade_count'] = (
      feed_stats['max_outside_bid_ask_trade_count'] / feed_stats['avg_outside_bid_ask_trade_count'])
  return feed_stats


def read_csv_into_df(csv_path):
  df = pandas.read_csv(csv_path, sep=',', header=0)
  return df


def main(argv):
  csv_path = FLAGS.csv_path
  assert csv_path, '--csv_path must be specified.'

  feed_stats = read_csv_into_df(csv_path)
  feed_stats = calc_and_gen_columns(feed_stats)
  calc_columns(feed_stats)
  plot_hist(feed_stats)

  return 0


if __name__ == '__main__':
  flags.DEFINE_string('csv_path', None, 'Input csv file path.')

  app.run(main)
